package com.林子祺.全名健康管理系统.view.servlet.content.comment;

import com.林子祺.全名健康管理系统.controller.content.collection.AddCollection;
import com.林子祺.全名健康管理系统.controller.loginandregister.Collection;
import com.林子祺.全名健康管理系统.entity.login.CollectionData;
import com.林子祺.全名健康管理系统.entity.login.CurrentCollectionData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//增加收藏
@WebServlet("/AddCollection")
public class AddCollect extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        boolean have = false;
        HttpSession session = req.getSession();
        String user = (String) session.getAttribute("user");
        String collection = req.getParameter("collectArticle");

        CollectionData.Refresh();
        Collection.pull(user);

        int number = 0;
        while(CollectionData.OutputCollectionData(number)){
            if(collection.equals(CurrentCollectionData.collection)){
                have = true;
                number++;
            }
            else
                number++;
        }

        if(have == true){

        }
        else
            AddCollection.add(user,collection);

        req.getRequestDispatcher("/CollectionData?user="+user).forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
